package public

import (
	"gitcode.com/G-YT/iHealth25/server/global"
	"gitcode.com/G-YT/iHealth25/server/model/common/response"
	"gitcode.com/G-YT/iHealth25/server/model/public"
	"gitcode.com/G-YT/iHealth25/server/service"
	"gitcode.com/G-YT/iHealth25/server/utils"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type OnlineServiceApi struct {
}

var onlineServiceService = service.ServiceGroupApp.PublicServiceGroup.OnlineServiceService

// GetQuestionnaire 获取问题
func (onlineServiceApi *OnlineServiceApi) GetQuestionnaire(c *gin.Context) {
	var po public.OnlineServiceQuestion
	err := c.ShouldBindJSON(&po)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	var request map[string]interface{}
	if po.Pid != 0 {
		request = map[string]interface{}{"pid": po.Pid}
	}
	if po.Tag != 0 {
		request = map[string]interface{}{"pid": po.Pid, "tag": po.Tag}
	}
	question, _ := onlineServiceService.FindQuestionByWhere(request, "q_id,pid,question,types,info")
	if err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		response.Result(200, question, "获取成功", c)
	}
}

// SubmitQuestionnaire 获取问题
func (onlineServiceApi *OnlineServiceApi) SubmitQuestionnaire(c *gin.Context) {
	var po public.OnlineServiceAnswer
	err := c.ShouldBindJSON(&po)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	uid := utils.GetUserID(c)
	//answer, _ := json.Marshal(map[string]interface{}{
	//	"info": po.Answer,
	//})
	//po.Answer = answer
	po.Uid = uid
	if err := onlineServiceService.SubmitQuestionnaire(&po); err != nil {
		global.GVA_LOG.Error("提交失败!", zap.Error(err))
		response.FailWithMessage("提交失败", c)
	} else {
		response.Result(200, "", "创建成功", c)
	}
}
